package xuanzhen.manage.controller;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import xuanzhen.dating.pojo.DatingUser;
import xuanzhen.dating.service.DatingService;
import xuanzhen.manage.dao.exp.ExpExcel;
import xuanzhen.pro.pojo.Relay;
import xuanzhen.pro.service.RelayService;
import xuanzhen.reservation.pojo.RedPacket;
import xuanzhen.reservation.pojo.Reservation;
import xuanzhen.reservation.service.RedPacketService;
import xuanzhen.reservation.service.ReservationService;


@Controller
@RequestMapping("/exp")
public class ExpExcelController {
	@Resource
	RelayService relayService;
	@Resource
	DatingService datingService;
	@Resource
	ReservationService reserService;
	@Resource
	RedPacketService redPacketService;
	/**
	 * 导出报名用户
	 */
	@RequestMapping(value = "/expUsers", method = RequestMethod.GET)
	public String sUser(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		List<Relay> uList= relayService.selectAll();
		String fileName = "Excel-用户报名" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";  
        String headStr = "attachment; filename=\"" + fileName + "\"";  
        /*response = getResponse(); */ 
        response.setContentType("APPLICATION/OCTET-STREAM");  
        response.setHeader("Content-Disposition", headStr);  
        OutputStream out = response.getOutputStream();
        //组装 Message.getString(
        String title = "报名用户信息表单.XZ";  
        String[] rowsName = new String[]{"序号","姓名","联系方式","性别","比赛编号","报名组别","T恤尺码","国籍","证件","出行方式","紧急联系人","报名时间","微信OPENID","订单状态"};  
    List<Object[]>  dataList = new ArrayList<Object[]>();  
    Object[] objs = null;  
    for (int i = 0; i < uList.size(); i++) {  
    	Relay us = uList.get(i);  
        objs = new Object[rowsName.length];  
        objs[0] = i+1;  
       /* objs[1] = us.getName()==null?"无":us.getName();  
        objs[2] = us.getContact()==null?"无":us.getContact();  
        String gend= changeGender(us.getGender());
        objs[3] = gend==null?"无":gend;  
        objs[4] = us.getRaceNo()==null?"无":us.getRaceNo();
       String raceG=changeSignType(us.getSignType(),us.getGender())+changeGroup(us.getRaceGroup());
        objs[5] = raceG==null?"无":raceG;
        objs[6] = us.gettShirt()==null?"无":us.gettShirt();
        objs[7] = us.getCountry()==null?"无":us.getCountry();
        String cID=changeCard(us.getCardType())+us.getCerNumber();
        objs[8] =cID==null?"无":cID;
        String tripM=changeTrip(us.getTripMode());
        objs[9] = tripM==null?"无":tripM;
        objs[10] = us.getLinkMan()==null?"无":us.getLinkMan()+"："+us.getLinkPhone()==null?"无":us.getLinkPhone();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String date = us.getCreatTime(); 
        objs[11] = date==null?"无":date;  
        objs[12] = us.getOpenid()==null?"无":us.getOpenid();
        String payState=changePay(us.getOrderState());
        objs[13] = payState==null?"无":payState;  */
        dataList.add(objs);  
    	}
        //导出
        ExpExcel ex = new ExpExcel(title, rowsName, dataList);
        ex.export(out);
			return null;
		
		
	}
	public String changeGroup(String raceGroup){
		if("A".equals(raceGroup)){
			return "10公里";
		}else if("B".equals(raceGroup)){
			return "10公里";
		}else{
			return "30公里";
		}
		
	}
	public String changeSignType(String signType,String gender){
		if("S".equals(signType)){
			if("W".equals(gender)){
				return "个人女子";
			}else{
				return "个人男子";
			}
		}else if("G".equals(signType)){
			return "团队";
		}else {
			return "团队";
		}
		
	}
	public String changeGender(String gender){
		if("M".equals(gender)){
			return "男";
		}else{
			return "女";
		}
	}
	public String changeCard(String cardType){
		if("ID".equals(cardType)){
			return "身份证：";
		}else{
			return "护照：";
		}
	}
	public String changeTrip(String tripMode){
		if("sd".equals(tripMode)){
			return "自驾";
		}else{
			return "公共交通";
		}
	}
	public String changePay(String orderState){
		if("payment".equals(orderState)){
			return "支付完成";
		}else{
			return "未支付";
		}
	}
	/**
	 * 导出11.11快闪交友报名用户
	 */
	@RequestMapping(value = "/expDatingUsers", method = RequestMethod.GET)
	public String sDatingUser(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		List<DatingUser> uList= datingService.selectAll();
		String fileName = "Excel-快闪交友用户报名" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";  
        String headStr = "attachment; filename=\"" + fileName + "\"";  
        /*response = getResponse(); */ 
        response.setContentType("APPLICATION/OCTET-STREAM");  
        response.setHeader("Content-Disposition", headStr);  
        OutputStream out = response.getOutputStream();
        //组装 Message.getString(
        String title = "快闪交友用户表.XZ";  
        String[] rowsName = new String[]{"序号","姓名","联系方式","性别","活动时间","是否会员","报名时间","来源"};  
    List<Object[]>  dataList = new ArrayList<Object[]>();  
    Object[] objs = null;  
    for (int i = 0; i < uList.size(); i++) {  
    	DatingUser us = uList.get(i);  
        objs = new Object[rowsName.length];  
        objs[0] = i+1; 
        objs[1] =us.getName();
        objs[2] = us.getPhone();
        if("M".equals(us.getGender())){
        	us.setGender("男");
        }else{
        	us.setGender("女");
        }
        objs[3] = us.getGender();
        objs[4] = us.getSignTime();
        if("Y".equals(us.getMember())){
        	us.setMember("是");
        }else{
        	us.setMember("否");
        }
        objs[5] = us.getMember();
        if(us.getCreateTime() == null){
        	us.setCreateTime("无");
        }
        objs[6] = us.getCreateTime();
        objs[7] = us.getSpread();
        dataList.add(objs);  
    	}
        //导出
        ExpExcel ex = new ExpExcel(title, rowsName, dataList);
        ex.export(out);
			return null;
		
		
	}
	/**
	 * 导出表用户
	 */
	@RequestMapping(value = "/expReserUsers", method = RequestMethod.GET)
	public String expReserUsers(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,Object>  parameter = new HashMap<String,Object>();
		List<Reservation> uList= reserService.queryLikeListByMap(parameter);
		String fileName = "Excel-约车用户" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";  
        String headStr = "attachment; filename=\"" + fileName + "\"";  
        /*response = getResponse(); */ 
        response.setContentType("APPLICATION/OCTET-STREAM");  
        response.setHeader("Content-Disposition", headStr);  
        OutputStream out = response.getOutputStream();
        //组装 Message.getString(
        String title = "约车用户表.XZ";  
        String[] rowsName = new String[]{"订单号","会员姓名","会员手机号","送车区域","送车地点","送车时间","备注","订单日期","订单时间","订单状态","调度反馈信息","送车司机","调度时间","调度完成时间","取消时间","渠道","爽约取消原因"};  
    List<Object[]>  dataList = new ArrayList<Object[]>();  
    Object[] objs = null;  
    for (int i = 0; i < uList.size(); i++) {  
    	Reservation us = uList.get(i);  
        objs = new Object[rowsName.length];  
        objs[0] = us.getOrderNo(); 
        objs[1] =us.getName();
        objs[2] = us.getPhone();
        objs[3] = us.getArea();
        objs[4] = us.getAddress();
        objs[5] = changeTime(us.getTime());
        if(us.getReRemark()!=null && !"".equals(us.getReRemark())){
        	objs[6] = us.getReRemark();
        }else{
        	objs[6] ="无";
        }
       /* objs[6] = (us.getReRemark()!=null?us.getReRemark():"");*/
        objs[7] = changeTimeOnlyDay(us.getCreateTime());
        objs[8] = changeTime(us.getCreateTime());
        objs[9] = changeState(us.getState());
        if(us.getRemark()!=null && !"".equals(us.getRemark())){
        	objs[10] = us.getRemark();
        }else{
        	objs[10] ="无";
        }
       /* objs[9] = (us.getRemark()!=null?us.getRemark():"无");*/
        if(us.getpName()!=null && !"".equals(us.getpName())){
        	objs[11] = us.getpName();
        }else{
        	objs[11] ="无";
        }
       /* objs[10] = (us.getpName()!=null?us.getpName():"无");*/
        if(us.getDistributeTime()!=null){
        	objs[12] = changeTime(us.getDistributeTime());
        }else{
        	objs[12] ="无";
        }
       /* objs[11] = (us.getDistributeTime()!=null?us.getDistributeTime():"--");*/
        if(us.getFinishTime()!=null){
        	objs[13] = changeTime(us.getFinishTime());
        }else{
        	objs[13] ="无";
        }
       /* objs[12] = (us.getFinishTime()!=null?us.getFinishTime():"--");*/
        if(us.getCancelTime()!=null){
        	objs[14] = changeTime(us.getCancelTime());
        }else{
        	objs[14] ="无";
        }
        /*objs[13] = (us.getCancelTime()!=null?us.getCancelTime():"--");*/
        objs[15] = changeSpread(us.getSpread());
        if(us.getCancelFeedback()!=null && !"".equals(us.getCancelFeedback())){
        	objs[16] = us.getCancelFeedback();
        }else{
        	objs[16] ="无";
        }
       /* objs[15] = (us.getCancelFeedback()!=null?us.getCancelFeedback():"无");*/
        dataList.add(objs);  
    	}
        //导出
        ExpExcel ex = new ExpExcel(title, rowsName, dataList);
        ex.export(out);
			return null;
	}
	/**
	 * 导出表用户
	 */
	@RequestMapping(value = "/expRedPackets", method = RequestMethod.GET)
	public String expRedPackets(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,Object>  parameter = new HashMap<String,Object>();
		List<RedPacket> uList= redPacketService.selectListByMap(parameter);
		String fileName = "Excel-RedPacket" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";  
        String headStr = "attachment; filename=\"" + fileName + "\"";  
        /*response = getResponse(); */ 
        response.setContentType("APPLICATION/OCTET-STREAM");  
        response.setHeader("Content-Disposition", headStr);  
        OutputStream out = response.getOutputStream();
        //组装 Message.getString(
        String title = "红包列表.XZ";  
        String[] rowsName = new String[]{"红包序号","手机号","红包名称","金额","创建时间"};  
    List<Object[]>  dataList = new ArrayList<Object[]>();  
    Object[] objs = null;  
    for (int i = 0; i < uList.size(); i++) {  
    	RedPacket us = uList.get(i);  
        objs = new Object[rowsName.length];  
        objs[0] = us.getId(); 
        objs[1] =us.getUnique();
        objs[2] = us.getName();
        objs[3] = us.getCount();
        objs[4] = changeTime(us.getCreateTime());
        dataList.add(objs);  
    	}
        //导出
        ExpExcel ex = new ExpExcel(title, rowsName, dataList);
        ex.export(out);
			return null;
	}
	public String changeSpread(String spread){
		String sp="微信分享";
		if(spread !=null){
			switch (spread) {
			case "1":
				sp="微信菜单";
				break;
			case "2":
				sp="微信文章";
				break;
			case "3":
				sp="微博";
				break;
			case "4":
				sp="协信星光";
				break;
			case "5":
				sp="龙湖";
				break;
			case "6":
				sp="东原1891";
				break;
			case "7":
				sp="车贴";
				break;
			case "8":
				sp="大屏幕";
				break;
			case "9":
				sp="公关媒体";
				break;
			case "10":
				sp="乐视";
				break;
			case "11":
				sp="短信";
				break;
			case "12":
				sp="微信";
				break;
			case "13":
				sp="微博";
				break;
			case "14":
				sp="公关媒体";
			case "15":
				sp="乐视";
				break;
			default:
				break;
			}
		}
		
		return sp;
	}
	public String changeState(String state){
		String sp="无";
		switch (state) {
		case "UNREAD":
			sp="未处理";
			break;
		case "RECEIVE":
			sp="未处理";
			break;
		case "REFUSE":
			sp="被拒绝";
			break;
		case "UNFINISH":
			sp="派送中";
			break;
		case "FINISH":
			sp="已完成";
			break;
		case "CANCEL":
			sp="客户取消";
			break;
		case "BEICANCEL":
			sp="爽约取消";
			break;
		default:
			break;
		}
		return sp;
	}
	public String changeTime(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(date);
		return nowTime;
	}
	public String changeTimeOnlyDay(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(date);
		return nowTime;
	}
		
	
}
